package swings.frames;

import swings.buttons.AddParamButton;
import swings.labels.ParamLabel;
import swings.others.CommonScrollPane;
import swings.panels.CommonComboBoxPanel;
import swings.panels.CommonPanel;
import utils.CommonUtils;
import utils.ImageUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @version V1.0
 * @Title: 编辑签名frame
 * @Package
 * @Description:
 * @author: runoob
 * @date:
 */
public class OperateSignFrame extends JFrame {

    private Container contentPane = this.getContentPane();
    private ParamLabel paramLabel;
    private CommonPanel main = new CommonPanel();
    private JButton saveBtn = new JButton("Save");
    private CommonScrollPane jScrollPane = new CommonScrollPane(0,5, 380,300);
    private AddParamButton addParamButton = new AddParamButton(10,45,350,30);
    private Color bgColor = CommonUtils.bgColor;
    private List<String> dataList;
    //使用计数器来记录当前窗口共有几个下拉列表,用来计算坐标和座map的key
    private int count = 1;
    //使用map来记录窗口中所有的下拉列表
    private Map<Integer, CommonComboBoxPanel> comboMap = new HashMap<>();

    /**
     * 构造方法
     */
    public OperateSignFrame(ParamLabel paramLabel, List<String> dataList) throws IOException {
        this.paramLabel = paramLabel;
        this.dataList = dataList;
        //初始化组件
        initComponent();
        //初始化按钮
        initBtn();
        //初始化窗口
        init();
    }

    /**
     * 初始化组件
     */
    public void initComponent() throws IOException {
        main.setLayout(null);
        saveBtn.setBounds(150,320,70,30);
        saveBtn.setFont(CommonUtils.getFont());
        CommonComboBoxPanel cache = new CommonComboBoxPanel(0,30,5,350,30,dataList,"",this);
        comboMap.put(0,cache);
        main.add(cache);
        main.setBorder(null);
        main.setBounds(0,5, 380,300);
        main.setPreferredSize(new Dimension(380,600));
        jScrollPane.getViewport().add(main);
        contentPane.setLayout(null);
        contentPane.add(saveBtn);
        contentPane.add(jScrollPane);
        contentPane.setBackground(bgColor);
    }

    /**
     * 初始化按钮
     */
    public void initBtn() {
        addParamButton.addActionListener(new addParam());
        //保存按钮监听器
        saveBtn.addActionListener(e -> {
            StringBuilder result = new StringBuilder();
            Set<Map.Entry<Integer, CommonComboBoxPanel>> entries = comboMap.entrySet();
            for (Map.Entry<Integer, CommonComboBoxPanel> entry : entries) {
                CommonComboBoxPanel cache = entry.getValue();
                result.append(cache.valueField.getText()).append(" + ");
            }
            paramLabel.valueField.setText(result.toString().substring(0, result.length()-3));
            dispose();
        });
        main.add(addParamButton);
    }

    /**
     * 初始化窗口
     */
    public void init(){
        this.setTitle("OperateSign");
        this.setIconImage(ImageUtils.logo);
        this.setLocation((CommonUtils.screenWidth-400)/2, (CommonUtils.screenHeight-220)/2);
        this.setSize(400,400);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }

    /**
     * 新增下拉列表监听器
     */
    private class addParam implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            addParam();
        }
    }

    /**
     * 新增下拉列表
     */
    private void addParam(){
        try {
            CommonComboBoxPanel cache = new CommonComboBoxPanel(count,30,5 + count*40,350,30,dataList,"",this);
            comboMap.put(count,cache);
            main.add(cache);
            addParamButton.setBounds(10,45 + count*40,350,30);
            count++;
            refreshFrame();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    /**
     *删除下拉列表
     */
    public void removeCombo(int symbol){
        //y轴坐标
        int y = 5;
        //清空组件
        main.removeAll();
        //从map中删除对应的下拉列表
        comboMap.remove(symbol);
        //用map中的数据重新加载下拉列表
        Set<Map.Entry<Integer, CommonComboBoxPanel>> entries = comboMap.entrySet();
        for (Map.Entry<Integer, CommonComboBoxPanel> entry : entries) {
            CommonComboBoxPanel cache = entry.getValue();
            cache.setY(y);
            main.add(cache);
            y += 40;
        }
        //总数-1
        count--;
        //重新加载添加按钮
        addParamButton.setBounds(10,5 + count*40,350,30);
        main.add(addParamButton);
        refreshFrame();
    }

    /**
     * 刷新窗口
     */
    public void refreshFrame(){
        this.invalidate();
        this.validate();
        this.repaint();
    }

}
